package com.ddtech.business.train.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ddtech.business.common.BaseService;
import com.ddtech.business.train.entity.TrainRecord;
import com.ddtech.business.train.mapper.TrainRecordMapper;
import com.ddtech.business.train.query.TrainRecordQuery;
import com.ddtech.business.train.service.TrainRecordService;
import com.ddtech.framework.system.model.SysUser;
import com.ddtech.framework.system.service.UserService;
import com.ddtech.utils.PageUtil;
import com.ddtech.utils.SecurityUtils;
import com.ddtech.utils.StringUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @version v1.0
 * @ClassName:TrainRecordServiceImpl description：
 * @author:wei_zhang(xiao)
 * @date: 2024-11-24 15:43
 */
@Service
public class TrainRecordServiceImpl  extends BaseService<TrainRecordMapper, TrainRecord> implements TrainRecordService {

    @Autowired
    private UserService userService;

    @Resource
    private TrainRecordMapper trainRecordMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject create(TrainRecord trainRecord) {
        trainRecord.init();
        trainRecord.setUserId(SecurityUtils.getUserId());
        trainRecord.setUserName(SecurityUtils.getName());
        //trainRecord.setTotalExperienceValue(0);
        SysUser user = userService.findByUserId(SecurityUtils.getUserId());
        int experienceValue =0;
        if(user.getTotalExperienceValue()!=null){
            experienceValue=user.getTotalExperienceValue();
        }
        int nowExperienceValue=experienceValue+(trainRecord.getExperienceValue()==null ? 0: trainRecord.getExperienceValue());
        user.setTotalExperienceValue(nowExperienceValue);
        user.setUserLevel(trainRecord.getUserLevel());
        JSONArray mergeArray = new JSONArray();
        JSONArray mergeArray1 = new JSONArray();
        if(StringUtils.isEmpty(user.getAchievement())){
            if(StringUtils.isNotEmpty(trainRecord.getAchievement())){
                user.setAchievement(trainRecord.getAchievement());
                mergeArray1.addAll(JSON.parseArray(trainRecord.getAchievement()));
            }
        }else {
            if(StringUtils.isNotEmpty(trainRecord.getAchievement())){
                JSONArray oldArray=JSON.parseArray(user.getAchievement());
                JSONArray newArray=JSON.parseArray(trainRecord.getAchievement());
                mergeArray.addAll(oldArray);
                mergeArray.addAll(newArray);
                List<JSONObject> distinctList = ListUtils.emptyIfNull(mergeArray).stream().map((e) -> (JSONObject) e).distinct().collect(Collectors.toList());
                mergeArray1.addAll(distinctList);
                user.setAchievement(mergeArray1.toJSONString());
            }
        }
        trainRecord.setTotalExperienceValue(nowExperienceValue);
        userService.update(user);
        this.saveOrUpdate(trainRecord);
        JSONObject json = new JSONObject();
        json.put("achievement",mergeArray1.size()==0 ? "" : mergeArray1.toJSONString());
        json.put("totalExperienceValue",nowExperienceValue);
        return  json;
    }


    @Override
    public Object getData(TrainRecordQuery trainRecordQuery) {
        IPage<TrainRecord> page = this.page(trainRecordQuery.populatePage(),trainRecordQuery.autoWrapper());
        return PageUtil.toPage(page.getRecords(),page.getTotal());
    }


    @Override
    public Object getReport(TrainRecordQuery trainRecordQuery) {
        IPage<Map<String, Object>> page = trainRecordMapper.getReport(trainRecordQuery.populatePage(), trainRecordQuery);
        return PageUtil.toPage(page.getRecords(), page.getTotal());
    }
}
